package com.futvan.z.erp.erp_search_engines.job;

import java.net.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

import com.futvan.z.erp.erp_search_engines_info.erp_search_engines_info;
import com.futvan.z.framework.util.HtmlUtil;
import com.futvan.z.job.ContentBean;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.futvan.z.framework.common.bean.z_form_table;
import com.futvan.z.framework.core.SuperService;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.DateUtil;
import com.futvan.z.framework.util.EmailUtil;
import com.futvan.z.framework.util.SpringUtil;

public class job_360_pc implements Job {

    public static String cookies;

    public void execute(JobExecutionContext context) throws JobExecutionException {
//关键字
        String keyinfo = String.valueOf(context.getJobDetail().getJobDataMap().get("keyinfo"));
        //模板ID
        String zid = String.valueOf(context.getJobDetail().getJobDataMap().get("zid"));
        //执行采集任务
        try {
            getCookie(keyinfo, 10, null, 0, zid);
        } catch (Exception e) {
            throw new JobExecutionException("采购360端舆情信息出错|" + e.getMessage());
        }
    }

    /**
     * 获取360PC舆情信息
     */
    public static void getContent(String search, int pages, String ip, int port, String zid) throws Exception {

        for (int i = 1; i <= 10; i++) {
            String url = "https://www.so.com/s?q=%E7%BE%8E%E7%BD%97%E5%9B%BD%E9%99%85&pn=" + i;
            String html = "";
            try {
                html = get(url, null, null, 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Element ul = HtmlUtil.getElement(html, ".result");
            Elements li = ul.select("li[class=res-list]");

            for (Element link : li) {

                ContentBean bean = new ContentBean();

                //标题
                if (null != link.getElementsByTag("h3").text()) {
                    if (!"".equals(link.getElementsByTag("h3").text())) {
                        bean.setTitle(link.getElementsByTag("h3").text());
                    } else {
                        Elements Title = link.select("h3[class=res-title]");
                        for (Element title1 : Title) {
                            String titles = title1.select("a").attr("em");
                            bean.setTitle(titles);
                        }
                    }
                }

                int num = z.getSqlSession().selectOne("selectoneint", "SELECT COUNT(*) FROM erp_search_engines_info WHERE title = '" + bean.getTitle() + "' AND search_engines_id = '" + zid + "'");

                if (num == 0) {
                    //真实链接
                    if (null != link.select("h3[class=res-title]")) {
                        Elements src = link.select("h3[class=res-title]");
                        for (Element href : src) {
                            String Src = href.select("a").attr("href");
                            bean.setSrc(Src);
                        }
                    }

                    //来源
                    if (null != link.select("p[class=res-linkinfo]")) {
                        Elements From = link.select("p[class=res-linkinfo]");
                        for (Element from : From) {
                            String comeFrom = from.select("cite").text();
                            bean.setFrom(comeFrom);
                        }
                    }

                    if (z.isNotNull(bean.getSrc())) {
                        String ipinfo = "";
                        if (z.isNotNull(ip) && port > 0) {
                            ipinfo = "|代理IP：" + ip + "|端口：" + port;
                        }
                        z.Log("【第" + i + "页】" + ipinfo + "|标题：" + bean.getTitle() + "|来源：" + bean.getFrom());

                        //保存网站信息
                        erp_search_engines_info info = new erp_search_engines_info();
                        info.setZid(z.newZid("erp_search_engines_info"));
                        info.setTitle(bean.getTitle());
                        info.setFrom_website(bean.getFrom());
                        info.setUrl(bean.getSrc());
                        info.setSseq("出现在第" + i + "页");
                        info.setSearch_engines_id(zid);
                        z.getSqlSession().insert("erp_search_engines_info_insert", info);
                    }


                }


                System.out.println(bean.toString());
            }
        }
    }

    /**
     * HTTP请求GET
     *
     * @param url
     * @param parameters
     * @return
     */
    public static String get(String url, Map<String, String> parameters, String ip, int port) throws Exception {
        //		try {
        CloseableHttpClient httpclient = null;
        if (z.isNotNull(ip) && port > 0) {
            HttpHost proxy = new HttpHost(ip, port, "http");
            RequestConfig defaultRequestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000)
                    .setSocketTimeout(5000)
                    .setConnectionRequestTimeout(5000)
                    .setProxy(proxy)
                    .build();

            //创建连接
            httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
        } else {
            RequestConfig defaultRequestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000)
                    .setSocketTimeout(5000)
                    .setConnectionRequestTimeout(5000)
                    .build();

            //创建连接
            httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
        }

        //封闭参数
        URIBuilder builder = new URIBuilder(url);
        if (parameters != null && parameters.size() > 0) {
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                builder.setParameter(entry.getKey(), entry.getValue());
            }
        }

        //创建请求
        HttpGet doGet = new HttpGet(builder.build());

        Random random = new Random();

        //User-Agent 为模拟chrome PC浏览器
        doGet.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; TencentTraveler 4.0)");
        //   doGet.addHeader("User-Agent", headersPool[random.nextInt(headersPool.length)]);
        doGet.addHeader("referrer", "no-referrer");
        System.out.println("QiHooGUID=" + cookies);
        doGet.addHeader("Cookie", "QiHooGUID=" + cookies);

        //如果有代理IP。设置X-Forwarded-For为代理IP，隐藏真实IP
        if (z.isNotNull(ip)) {
            doGet.setHeader("X-Forwarded-For", ip);
        }

        //执行
        HttpResponse response = httpclient.execute(doGet);
        int status = response.getStatusLine().getStatusCode();
        if (status >= 200 && status < 300) {
            HttpEntity entity = response.getEntity();
            //如果返回信息不为空，直接输出。
            String html = entity != null ? EntityUtils.toString(entity, "utf-8") : "";
            return html;
        } else {
            z.Error("HTTP ERROR : " + status);
            return "";
        }
    }


    /**
     * 获取Cookie
     */
    public static void getCookie(String search, int pages, String ip, int port, String zid) {

        String cookieUrl = "https://www.so.com/s?q=%E7%BE%8E%E7%BD%97%E5%9B%BD%E9%99%85&pn=" + 1;

        try {
            CookieManager manager = new CookieManager();
            manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
            CookieHandler.setDefault(manager);
            URL url = new URL(cookieUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.getHeaderFields();
            CookieStore store = manager.getCookieStore();
            List<HttpCookie> lCookies = store.getCookies();
            for (HttpCookie cookie : lCookies) {
                if ("QiHooGUID".equals(cookie.getName())) {
                    cookies = cookie.getValue();
                    getContent(search, pages, null, 0, zid);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
